import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:rammus/rammus.dart' as rammus;

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String? _platformVersion = 'Unknown';

  @override
  initState() {
    super.initState();
    initPlatformState();
    if (!Platform.isAndroid) {
      rammus.configureNotificationPresentationOption();
    }
    _createNotificationChannel();
    _initListener();
  }

  void _initListener() {
    rammus.initCloudChannelResult.listen((data) {
      print(
          "----------->init successful ${data.isSuccessful} ${data.errorCode} ${data.errorMessage}");
    });

    rammus.onNotification.listen((data) {
      print("----------->notification here ${data.summary}");
      setState(() {
        _platformVersion = data.summary;
      });
    });
    rammus.onNotificationOpened.listen((data) {
      print("-----------> ${data.summary} 被点了");
      setState(() {
        _platformVersion = "${data.summary} 被点了";
      });
    });

    rammus.onNotificationRemoved.listen((data) {
      print("-----------> $data 被删除了");
    });

    rammus.onNotificationReceivedInApp.listen((data) {
      print("-----------> ${data.summary} In app");
    });

    rammus.onNotificationClickedWithNoAction.listen((data) {
      print("${data.summary} no action");
    });

    rammus.onMessageArrived.listen((data) {
      print("received data -> ${data.content}");
      setState(() {
        _platformVersion = data.content;
      });
    });
  }

  //创建通知通道
  void _createNotificationChannel() {
    var channels = <rammus.NotificationChannel>[];
    channels.add(rammus.NotificationChannel(
      "mashanglai",
      "马上来",
      "马上来",
      importance: rammus.AndroidNotificationImportance.MAX,
    ));
    channels.add(rammus.NotificationChannel(
      "mashanglai_notification",
      "马上来通知",
      "马上来通知消息",
      importance: rammus.AndroidNotificationImportance.MAX,
    ));
    rammus.setupNotificationManager(channels);
  }

  getDeviceId() async {
    var deviceId = await rammus.deviceId;
    print("deviceId:::$deviceId");
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String? platformVersion;
    if (!mounted) return;
    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            Center(
              child: Text('Running on: $_platformVersion\n'),
            ),
            TextButton(
              onPressed: () {
                getDeviceId();
              },
              child: Text("getDeviceId"),
            ),
            TextButton(
              onPressed: () {
                rammus.register();
                rammus.registerHW();
                rammus.registerHonor();
                rammus.registerOPPO("6xbMancXxs8444O8oOgs8o04o",
                    "D187a88764269197b5149d62fCC267FA");
                rammus.registerVIVO(
                    "100134924", "de22f9d1c7edd3c4ba2ab71afaa956f5");
                rammus.registerXM("2882303761518514296", "5811851489296");
              },
              child: Text("register"),
            ),
            TextButton(
              onPressed: () {
                rammus.initPushService();
                rammus.addAlias("18660151196");
                rammus.bindAccount("18660151196");
              },
              child: Text("bindAccount"),
            ),
          ],
        ),
      ),
    );
  }
}
